export function GetRandomNum(Min,Max){   
    var Range = Max - Min;   
    var Rand = Math.random();   
    return(Min + Math.round(Rand * Range));   
}   


var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];


export function Base64Encode(msg){
   var res =  Buffer.from(msg).toString('base64')
   return res
}



export function DictSort(data) {
  var res = {};
  for (let key of Object.keys(data).sort()) {
    res[key] = data[key]
  }
  return res

}

export function StrToJson(msg){
  var res = {}
  try {
    res = JSON.parse(msg)
  } catch(e) {
    console.log(e);
  }
  return res
}

export function DictSortStr(data) {
  var res = "";
  for (let key of Object.keys(data).sort()) {
    res += data[key]
  }
  return res

}

export function urlEncode(str){
    let res = encodeURIComponent(str);
    return res
}

export function urlDecode(str){
    let res = decodeURIComponent(str);
    return res
}

export function Base64Decode(msg){
  return Buffer.from(msg, 'base64').toString('utf-8')
}


export function GetRandomStr(n) {
     var res = "";
     for(var i = 0; i < n ; i ++) {
         var id = Math.ceil(Math.random()*35);
         res += chars[id];
     }
     return res;
}

export  function ReplaceAll(find,replace, str) {
  var find = find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  return str.replace(new RegExp(find, 'g'), replace);
}

//异或加密
export function XorEncodeStr(message, key) {
  // console.log(message);
  const arr = [];
  var ml = message.length
  var kl = key.length
  for (let i = 0; i < ml; i++) {

    const  m = message.substr(i, 1);//parseInt(message.substr(i, 1), ml);

    
    const k = key.substr(parseInt(i%kl),1);//parseInt(key.substr(i, 1), kl);
   
    const asstr = k.charCodeAt() ^ m.charCodeAt()
    arr.push(String.fromCharCode(asstr));
  }
  return arr.join('');
}

//异或解密
export function XorDecodeStr(message, key) {
  const arr = [];
  var ml = message.length
  var kl = key.length
  for (let i = 0; i < ml; i++) {
    const  m = message.substr(i, 1);
    // console.log(m);
    const k = key.substr(parseInt(i%kl),1);

    const asstr =  m.charCodeAt() ^ k.charCodeAt()
    // console.log(asstr,String.fromCharCode(asstr));
    arr.push(String.fromCharCode(asstr));
  }
  try {
    return decodeURIComponent(escape(arr.join('')))
  } catch(e) {
    return arr.join('')
  }
}



export function XorDecodeStrByte(message, key) {
  const arr = [];
  const t = []
  if (message == undefined) {
    return arr.join('')
  }
  var ml = message.length
  var kl = key.length


  for (let i = 0; i < ml; i++){
    const asstr =  message[i] ^ key[parseInt(i%kl)]
    t.push(asstr)
    arr.push(String.fromCharCode(asstr));
  }
  
  try {
    return decodeURIComponent(escape(arr.join('')))
  } catch(e){
    return arr.join('')
  }
  
}


export function stringToHex(str){
  var val="";
  for(var i = 0; i < str.length; i++){
    if(val == "") {
      val= str.charCodeAt(i).toString(16);
    }else{
      val+= "," + str.charCodeAt(i).toString(16);
    }

  }
  return val;

}

export function HexToten(value){
    var res = parseInt(value,16);
    return res;
}